package edu.hawaii.halealohacli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.client.WattDepotClientException;
import edu.hawaii.halealohacli.HaleAlohaCli;

/**
 * Ensures that CurrentPower is returning the correct data.
 * 
 * @author Andrea Connell
 * @author Zach Tomaszewski
 */
public class TestCurrentPower {

  private String url = HaleAlohaCli.URL;

  /**
   * Run CurrentPower on a valid tower.
   * @throws WattDepotClientException If can't connect to server during test
   */
  @Test
  public void testValid() throws WattDepotClientException {
    WattDepotClient client = new WattDepotClient(url);
    String[] args = { "Lehua" };
    CurrentPower command = new CurrentPower();
    command.initialize(args, client);
    assertEquals("Command Name", command.getCommandName(), "current-power");
    command.execute();
    
    CurrentPower.Data data = command.getCurrentPower();
    assertEquals("source name saved correctly", "Lehua", data.getSource());
    assertTrue("power level sanity check", data.getPower() > 0); 
  }

  /**
   * Run CurrentPower on an invalid tower.
   */
  @Test
  public void testSource() {
    WattDepotClient client = new WattDepotClient(url);
    String[] args = { "Ilima2" };
    CurrentPower command = new CurrentPower();
    command.initialize(args, client);
    assertEquals("Invalid Source", command.getResults(), "Ilima2 is not a valid source name.");
  }

  /**
   * Run CurrentPower with invalid arguments.
   */
  @Test
  public void testArguments() {
    WattDepotClient client = new WattDepotClient(url);
    String[] args = { "Ilima", "11/16/2011" };
    CurrentPower command = new CurrentPower();
    command.initialize(args, client);
    command.execute();
    assertEquals("Invalid Arguments", command.getResults(),
        "Invalid arguments for " + command.getCommandName() + ".\n" + command.getCommandUsage());
  }

  /**
   * Run CurrentPower with a null or invalid client.
   */
  @Test
  public void testClient() {
    WattDepotClient client = new WattDepotClient(url + "error");
    String[] args = { "Ilima" };
    CurrentPower command = new CurrentPower();
    command.initialize(args, null);
    assertEquals("Null Client", command.getResults(), "Invalid WattDepot client");

    command.initialize(args, client);
    assertEquals("Invalid URL", command.getResults(), "Invalid WattDepot client");
  }

}
